package com.mySampleApplication.client.components;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.SelectionAppearance;
import com.smartgwt.client.types.SelectionStyle;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.*;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.SelectionUpdatedEvent;
import com.smartgwt.client.widgets.grid.events.SelectionUpdatedHandler;
import com.smartgwt.client.widgets.layout.HLayout;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


public class SListGrid {

    private static ListGrid plistGrid;


    public static ListGrid getPlistGrid() {
        return plistGrid;
    }

    public static void setPlistGrid(ListGrid plistGrid) {
        SListGrid.plistGrid = plistGrid;
    }

    public static Map<String,Object> getListGrid() {
        Map<String,Object> map =new HashMap<>();
        // 创建一个列表网格控件
        ListGrid listGrid =getOp();;

        ListGrid finalListGrid = listGrid;
        listGrid.setWidth100(); // 设置宽度为100%
        listGrid.setHeight100(); // 设置高度为100%
//        listGrid.setWidth(1024);
//        listGrid.setHeight(224);
        // 设置列表网格显示网格线
        listGrid.setShowAllRecords(false); // 显示所有记录
        // 开启分页功能
        listGrid.setDataPageSize(10); // 每页显示的数据条数
        //listGrid.setShowFilterEditor(true); // 显示过滤编辑器
        listGrid.setSelectionType(SelectionStyle.MULTIPLE);//开启多选框
        //listGrid.setShowRowNumbers(true); // 显示行号
        //listGrid.setPaginationMode(PaginationMode.LOCAL); // 设置分页模式为本地分页
        //允许用户编辑行内容
        //listGrid.setCanEdit(true);
        //listGrid.setEditEvent(ListGridEditEvent.DOUBLECLICK); // 双击进行编辑
        listGrid.setModalEditing(true);
        listGrid.setVirtualScrolling(false);
        listGrid.setShowRecordComponents(true);
        listGrid.setShowRecordComponentsByCell(true);
        //listGrid.setCanRemoveRecords(true);


        ListGridField selectedCountriesField = new ListGridField("name", "Selected");
        selectedCountriesField.setWidth(70);
        listGrid.setFields(selectedCountriesField);
        listGrid.setSelectionType(SelectionStyle.SIMPLE);
        listGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);

        // 定义列表网格的列
        ListGridField idField = new ListGridField("id", "序号");
        idField.setWidth(50);
        ListGridField nameField = new ListGridField("name", "名称");

        ListGridField loginField = new ListGridField("loginName", "登录名");

        ListGridField genderField = new ListGridField("gender", "性别");

        ListGridField ageField = new ListGridField("age", "年龄");

        ListGridField emailField = new ListGridField("email", "邮箱");

        ListGridField actionField = new ListGridField("actionField", "操作");

        listGrid.setFields(idField, nameField,loginField,genderField,ageField, emailField,actionField);

        // 创建按钮和事件处理
        Button addButton = new Button("Add");
        addButton.setIcon("[SKIN]/actions/add.png"); // 设置图标路径
        addButton.setShowRollOver(true);
        addButton.setShowDown(true);
        addButton.addClickHandler(event -> showAddDialog(finalListGrid));

        Button EditButton = new Button("Edit");
        // 禁用按钮
        EditButton.setDisabled(true);
        EditButton.setIcon("[SKIN]/actions/edit.png"); // 设置图标路径
        EditButton.addClickHandler(event -> showEditDialog(finalListGrid));


        Button removeButton = new Button("Remove");
        removeButton.setIcon("[SKIN]/actions/remove.png"); // 设置图标路径
//        removeButton.setAutoFit(true);
        // 禁用按钮
        removeButton.setDisabled(true);
        removeButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                ListGridRecord[] selectedRecord = finalListGrid.getSelectedRecords();
                if(selectedRecord != null && selectedRecord.length>0) {
                    Arrays.stream(selectedRecord).forEach(a->{
                        finalListGrid.removeData(a);
                        removeButton.setDisabled(true);
                    });

                } else {
                    SC.say("Select a record before performing this action");
                }
            }

        });

        Button qButton = new Button("其他表单");
        //qButton.setIcon("[SKIN]/actions/edit.png"); // 设置图标路径
        qButton.addClickHandler(event -> showqDialog(finalListGrid));


        HLayout buttonLayout = new HLayout();
        buttonLayout.setMembersMargin(10);
        buttonLayout.setAlign(Alignment.LEFT);
        buttonLayout.addMembers(addButton, EditButton,removeButton,qButton);

        //单选事件
        //listGrid.setData(listGrid.getSelectedRecord());
        listGrid.addSelectionUpdatedHandler(new SelectionUpdatedHandler() {
            public void onSelectionUpdated(SelectionUpdatedEvent event) {
                 if(listGrid.getSelectedRecords().length>0){
                    removeButton.setDisabled(false);
                     if(listGrid.getSelectedRecords().length==1){
                         EditButton.setDisabled(false);
                     }else {
                         EditButton.setDisabled(true);
                     }
                }else {
                     removeButton.setDisabled(true);
                     EditButton.setDisabled(true);
                 }
                //SC.say(listGrid.getSelectedState());
            }
        });


        setPlistGrid(listGrid);
        map.put("listGrid",listGrid);
        map.put("buttonLayout",buttonLayout);
        return map;

    }

    private static ListGrid getOp(){
     return new ListGrid() {
            @Override
            protected Canvas createRecordComponent(final ListGridRecord record, Integer colNum) {

                String fieldName = this.getFieldName(colNum);

                if (fieldName.equals("actionField")) {
                    HLayout recordCanvas = new HLayout(2);
                    recordCanvas.setHeight(22);
                    recordCanvas.setWidth100();
                    recordCanvas.setAlign(Alignment.CENTER);
                    ImgButton editImg = new ImgButton();
                    editImg.setShowDown(false);
                    editImg.setShowRollOver(false);
                    //editImg.setLayoutAlign(Alignment.CENTER);
                    editImg.setSrc("[SKIN]/actions/edit.png");
                    editImg.setPrompt("编辑");
                    editImg.setHeight(16);
                    editImg.setWidth(16);
                    editImg.setRight(10);
                    editImg.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            editRecord(record,event);
                            //SC.say("Edit Comment Icon Clicked for country : " + record.getAttribute("id"));
                        }
                    });

                    ImgButton chartImg = new ImgButton();
                    chartImg.setShowDown(false);
                    chartImg.setShowRollOver(false);
                    //chartImg.setAlign(Alignment.CENTER);
                    chartImg.setSrc("[SKIN]/actions/remove.png");
                    chartImg.setPrompt("删除");
                    chartImg.setHeight(16);
                    chartImg.setWidth(16);
                    chartImg.addClickHandler(new ClickHandler() {
                        public void onClick(ClickEvent event) {
                            //deleteRecord(record,event);
                            //editRecord1(event);
                            SC.ask("是否删除该条数据?", new BooleanCallback() {
                                public void execute(Boolean value) {
                                    if (value != null && value) {
                                        deleteRecord(record,event);
                                    } else {

                                    }
                                }
                            });
                        }
                    });

                    recordCanvas.addMember(editImg);
                    recordCanvas.addMember(chartImg);
                    return recordCanvas;
                } else {
                    return null;
                }

            }
        };
    }

    private static void showqDialog(ListGrid listGrid) {
        Dialog dialog = SDialog.createDialog(1400,650,"表单组件", null,listGrid);
        dialog.show();
    }


    private static void showAddDialog(ListGrid listGrid) {
//        Dialog dialog = SDialog.createDialog("Add Item", null,listGrid);
        Window dialog=SShowWindow.getShowWindow(700,350,"Add Item", null,listGrid);
        dialog.show();
    }



    private static void showEditDialog(ListGrid listGrid) {
        ListGridRecord selectedRecord = listGrid.getSelectedRecord();
        if (selectedRecord != null) {
            //Dialog dialog = SDialog.createDialog("Edit Item", selectedRecord,listGrid);
            Window dialog=SShowWindow.getShowWindow(700,350,"Edit Item", selectedRecord,listGrid);
            dialog.show();
        } else {
            SC.say("Please select an item to edit.");
        }
    }

    // 编辑记录函数
    private static void editRecord(ListGridRecord record, ClickEvent event) {
        Window dialog=SShowWindow.getShowWindow(700,350,"Edit Item", record,getPlistGrid());
        dialog.show();
        // 执行编辑操作
    }

    // 删除记录函数
    private static void deleteRecord(ListGridRecord record, ClickEvent event) {
        ListGrid listGrid = getPlistGrid();
        listGrid.removeData(record);
        // 执行删除操作
    }
}
